#include "threadpool.h"

//队列初始化
int queue_init(taskQueue_t* queue){
   memset(queue,0,sizeof(taskQueue_t)); 
   return 0;
}

//入队
int enqueue(taskQueue_t* queue, Elemtype data){
    node_t* newNode = (node_t*)calloc(1,sizeof(node_t));
    if(newNode == NULL){
        printf("calloc failed!\n");
        return -1;
    }
    newNode->netfd = data;
    if(queue->queueSize == 0){
        queue->front = newNode;
        queue->rear = newNode;
        queue->queueSize++;
        return 0;
    }
    queue->rear->next = newNode;
    queue->rear = newNode;
    queue->queueSize++;
    return 0;

}

//出队
int dequeue(taskQueue_t* queue){
    node_t* curr = queue->front;
    queue->front = queue->front->next;
    if(queue->queueSize == 1){
        queue->rear = NULL;
    }
    free(curr);
    queue->queueSize--;
    return 0;
}

//线程池初始化
int thread_pool_init(threadPool_t* pool){
    pthread_mutex_init(&pool->mutex, NULL);
    pthread_cond_init(&pool->cond, NULL);
    queue_init(&pool->taskQueue);
    pool->threadNum = THREADNUM;
    pool->childThreadSet = (pthread_t*)calloc(pool->threadNum,sizeof(pthread_t));
    if(pool->childThreadSet == NULL){
        printf("calloc failed!\n");
        return -1;
    }
    pool->exitFlag = 0;
    return 0;
}

